<template>
  <div>
    <el-tree ref="tree" :data="treeData" :show-checkbox="showCheckbox" @check-change="checkChange" @node-click="getCheckedNode"
             default-expand-all></el-tree>
  </div>
</template>

<script>
import {deptUserTree} from "@/api/system/dept";

export default {
  data() {
    return {
      treeData: [],
    }
  },
  props: {
    projectId: {
      type: Number,
      default: null
    },
    showCheckbox: {
      type: Boolean,
      default: false
    }
  },
  created() {
    this.getDeptTree()
  },
  watch: {
    projectId() {
      this.getDeptTree()
    }
  },
  methods: {
    /** 查询部门人员树结构 */
    getDeptTree() {
      // 判断 projectId 是否是 Number类型
      if (typeof this.projectId !== 'number') {
        return;
      }
      deptUserTree(this.projectId).then(response => {
        this.treeData = response.data.children;
      })
    },
    checkChange() {
      let userIds = [];
      this.$refs.tree.getCheckedNodes().forEach(data => {
        if (data.data !== undefined){
          userIds.push(data.id);
        }
      });
      this.$emit("getData", userIds);
    },
    getCheckedNode(data,node) {
      if (this.showCheckbox === true) {
      return;
      }
      if (node.level===4){
        this.$emit("getData", data);
      }
    },
  }
}

</script>

<style scoped>

</style>
